package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;

public class GestureActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture);
        GestureLibrary library = GestureLibraries.fromFile("/sdcard/gestures");
        GestureOverlayView gestureOverlayView = (GestureOverlayView) findViewById(R.id.overlay);
        gestureOverlayView.addOnGesturePerformedListener((overlay, gesture) -> {
            boolean loadStatus = library.load();
            if (loadStatus) {
                ArrayList<Prediction> predictions = library.recognize(gesture);
                if (!predictions.isEmpty()) {
                    Prediction prediction = predictions.get(0);
                    if (prediction.score > 4) {
                        Toast.makeText(this, prediction.name, Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(this, "手势匹配不成功", Toast.LENGTH_LONG).show();
                    }
                }
            } else {
                Toast.makeText(this, "手势库加载失败", Toast.LENGTH_LONG).show();
            }
        });
    }
}